//
//  VCJourney.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/20.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCJourney: VCBase {
    
    // 行程头部
    private lazy var tripHeaderView:TripHeaderView = {
        let ui = TripHeaderView()
        ui.backgroundColor = UIColor.white
        ui.frame = CGRect(x: 0, y: realHeight, width: SCREEN_WIDTH, height: MARGIN60)
        return ui
    }()
    
    // 行程汇总
    private lazy var tripSummaryView:TripSummaryView = {
        let ui = TripSummaryView()
        ui.backgroundColor = UIColor.white
        ui.frame = CGRect(x: 0, y: tripHeaderView.frame.maxY, width: SCREEN_WIDTH, height: MARGIN80)
        return ui
    }()
    
    // 待出行、已结束
    private lazy var journeySegmentView: JourneySegmentView = {
        let ui = JourneySegmentView()
        ui.delegate = self
        ui.backgroundColor = UIColor.white
        ui.frame = CGRect(x: 0, y: tripSummaryView.frame.maxY + MARGIN10, width: SCREEN_WIDTH, height: MARGIN60)
        return ui
    }()
    
    // 订单显示
    private lazy var orderInfoMangerView: OrderMangerView = {
        let ui = OrderMangerView()
        let uiY = journeySegmentView.frame.maxY + MARGIN10
        let uiH = SCREEN_HEIGHT - uiY - getTabBarHeight()
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: uiH)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
       
        init_data()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCJourney {
    
    private func requestOrders(orderstatus:Int,isSummary:Bool = false) -> Void {
        guard let userid = UserDefaults.standard.value(forKey: SP_USERID) else {
            self.show(text: "本地数据获取失败，请使用账号密码重新登录")
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(userid, forKey: SP_USERID)
        request.setValue(orderstatus, forKey: SP_ORDERSTATUS)
        if isSummary {
            request.setValue(UserDefaults.standard.string(forKey: SP_USERNAME), forKey: SP_USERNAME)
            request.setValue(UserDefaults.standard.string(forKey: SP_IDNUMBER), forKey: SP_IDNUMBER)
        }
        // 请求
        self.doPost(api: "search/order", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.orderInfoMangerView.isHidden = true
                return
            }
            self.orderInfoMangerView.isHidden = false
            // 解析数据
            let dict = dict.value(forKey: SP_BODYREQUEST) as! NSDictionary
            // 转化数据
            var temp = [OrderInfo]()
            if let arrs = dict.value(forKey: "orders") as? NSArray{
                for item in arrs {
                    let order = OrderInfo(dict: item as! [String : Any])
                    temp.append(order)
                }
            }
            self.orderInfoMangerView.orders = temp
            // 行程汇总
            if isSummary {
                guard let summary = dict.value(forKey: "summary") as? NSDictionary else {
                    return
                }
                let mileage = String(describing:summary.value(forKey: "mileage") as! Int)
                let miletime = String(describing:summary.value(forKey: "miletime") as! Double)
                let milenumber = String(describing:summary.value(forKey: "milenumber") as! Int)
                self.tripSummaryView.tripSummary = [miletime,milenumber,mileage]
                // 票价
                let ticketprice = summary.value(forKey: "ticketprice") as! Int
                self.tripHeaderView.ticketPrice = ticketprice
            }
        }
    }
}

// MARK: 代理、回调
extension VCJourney: JourneySegmentViewDelegate {
    
    // 待出行
    func journey_will() {
        requestOrders(orderstatus: 0)
    }
    
    // 已结束
    func journey_finish() {
        requestOrders(orderstatus: 1)
    }
}



// MARK: 初始化
extension VCJourney {
    
    private func init_event() -> Void {
        orderInfoMangerView.complete = {item in
            let vc = VCTicketInfomation()
            vc.orderInfo = item
            self.pushVc(title: "订单详情", vc: vc)
        }
    }
    
    private func init_data() -> Void {
        let status = journeySegmentView.selectIndex()
        requestOrders(orderstatus: status,isSummary: true)
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(tripHeaderView)
        view.addSubview(tripSummaryView)
        view.addSubview(journeySegmentView)
        view.addSubview(orderInfoMangerView)
    }
    
}
